package com.hzhr.iot.device.service;

import com.hzhr.iot.core.annotation.ApiImplicitInternalParams;
import com.hzhr.iot.core.annotation.CygQuery;
import com.hzhr.iot.core.annotation.CygQuerys;
import com.hzhr.iot.core.annotation.CygService;
import com.hzhr.iot.core.constant.AccessModeEnum;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.constant.Device_ErrorsEnum;
import com.hzhr.iot.device.convertor.custom.IotDeviceCustomConvertor;
import com.hzhr.iot.device.convertor.custom.RegulatorCollectDataCustomConvertor;
import com.hzhr.iot.device.dao.IotDeviceDao;
import com.hzhr.iot.device.dao.RegulatorCollectDataDao;
import com.hzhr.iot.device.entity.IotDeviceEntity;
import com.hzhr.iot.device.entity.RegulatorCollectDataEntity;
import com.hzhr.iot.device.entity.ext.IotDeviceEntityExt;
import com.hzhr.iot.device.entity.ext.RegulatorCollectDataEntityExt;
import com.hzhr.iot.device.model.pressureMonitor1.PressureMonitor1GetModel;
import com.hzhr.iot.device.model.pressureMonitor1.PressureMonitor1Model;
import com.hzhr.iot.device.model.pressureMonitor1_collectData.PressureMonitor1_CollectDataGetModel;
import com.hzhr.iot.device.model.pressureMonitor1_collectData.PressureMonitor1_CollectDataModel;
import com.hzhr.iot.device.queryModel.custom.IotDeviceCustomQueryModel;
import com.hzhr.iot.device.queryModel.custom.RegulatorCollectDataCustomQueryModel;
import com.hzhr.iot.device.service.base.BaseService;
import com.hzhr.iot.user.sdk.model.dept.DeptModel;
import com.hzhr.iot.user.sdk.service.User_DeptService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@CygService(entity = RegulatorCollectDataEntity.class, model = PressureMonitor1_CollectDataModel.class)
@CygQuerys({
        @CygQuery(name = "_greaterthanorequal_collectTime", query = "greaterthanorequal_collectTime", type = Date.class),
        @CygQuery(name = "_greaterthan_collectTime", query = "greaterthan_collectTime", type = Date.class),
        @CygQuery(name = "_lessthanorequal_collectTime", query = "lessthanorequal_collectTime", type = Date.class),
        @CygQuery(name = "_lessthan_collectTime", query = "lessthan_collectTime", type = Date.class),
        @CygQuery(name = "_equal_collectTime", query = "equal_collectTime", type = Date.class),


})
public class PressureMonitor1_CollectDataService extends BaseService {
    @Autowired
    RegulatorCollectDataCustomConvertor regulatorCollectDataCustomConvertor;
    @Autowired
    RegulatorCollectDataDao regulatorCollectDataDao;
    @Autowired
    IotDeviceDao iotDeviceDao;

    //region javadoc注释 : get
    /**
     * 查询加装在用户管辖范围内的调压箱柜上的压力检测仪所采集的数据(根据采集时间降序排列) <br/><br/>
     * 步骤 : <br/>
     * 1. 查询压力检测仪：查询加装在用户管辖范围内的调压箱柜上的压力检测仪，如果不存在则抛出异常“压力监测仪不存在” <br/>
     * 2. 查询压力检测仪所采集的数据(根据采集时间降序排列) <br/>
     * 3. 返回信息 <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _show_total : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示总数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _show_info : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示详情;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _show_dept : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示部门信息;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _page : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 页数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _count : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 每页数量;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _greaterthanorequal_collectTime : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where PressureMonitor1Model.collectTime >= #{_greaterthanorequal_collectTime};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : long;<br/>
     * _greaterthan_collectTime : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where PressureMonitor1Model.collectTime > #{_greaterthan_collectTime};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : long;<br/>
     * _lessthanorequal_collectTime : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where PressureMonitor1Model.collectTime <= #{_lessthanorequal_collectTime};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : long;<br/>
     * _lessthan_collectTime : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where PressureMonitor1Model.collectTime < #{_lessthan_collectTime};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : long;<br/>
     * _equal_collectTime : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where PressureMonitor1Model.collectTime = #{_equal_collectTime};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : long;<br/><br/>
     * @throws ServiceException <br/>
     * PRESSUREMONITOR1_COLLECTDATASERVICE_GET_201 : <br/>&nbsp&nbsp&nbsp&nbsp异常类型 : 业务流程异常<br/>&nbsp&nbsp&nbsp&nbsp异常业务 : 查询压力检测仪<br/>&nbsp&nbsp&nbsp&nbsp异常描述 : 压力监测仪不存在<br/>
     * @see PressureMonitor1_CollectDataGetModel result <br/>
     */ 
    //endregion
    //region 说明 : get
    //查询加装在用户管辖范围内的调压箱柜上的压力检测仪所采集的数据(根据采集时间降序排列)
    //步骤 :
    //1. 查询压力检测仪：查询加装在用户管辖范围内的调压箱柜上的压力检测仪，如果不存在则抛出异常“压力监测仪不存在”
    //2. 查询压力检测仪所采集的数据(根据采集时间降序排列)
    //3. 返回信息
    //endregion
    //region 参数 : get
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "greaterthanorequal_collectTime", value = "筛选条件 : where PressureMonitor1Model.collectTime >= #{greaterthanorequal_collectTime}",required = false, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "greaterthan_collectTime", value = "筛选条件 : where PressureMonitor1Model.collectTime > #{greaterthan_collectTime}",required = false, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "lessthanorequal_collectTime", value = "筛选条件 : where PressureMonitor1Model.collectTime <= #{lessthanorequal_collectTime}",required = false, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "lessthan_collectTime", value = "筛选条件 : where PressureMonitor1Model.collectTime < #{lessthan_collectTime}",required = false, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "equal_collectTime", value = "筛选条件 : where PressureMonitor1Model.collectTime = #{equal_collectTime}",required = false, dataType = "long", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_show_dept", value = "是否展示部门信息", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_greaterthanorequal_collectTime", value = "筛选条件 : where PressureMonitor1Model.collectTime >= #{_greaterthanorequal_collectTime}",required = false, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "_greaterthan_collectTime", value = "筛选条件 : where PressureMonitor1Model.collectTime > #{_greaterthan_collectTime}",required = false, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "_lessthanorequal_collectTime", value = "筛选条件 : where PressureMonitor1Model.collectTime <= #{_lessthanorequal_collectTime}",required = false, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "_lessthan_collectTime", value = "筛选条件 : where PressureMonitor1Model.collectTime < #{_lessthan_collectTime}",required = false, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "_equal_collectTime", value = "筛选条件 : where PressureMonitor1Model.collectTime = #{_equal_collectTime}",required = false, dataType = "long", paramType = "query"),

    })
    //endregion
    //region 请求 : get
    @RequestMapping(value = "/pressure-monitor1s/{pressureMonitor1Id}/collect-datas", method = RequestMethod.GET)
    //endregion
    public PressureMonitor1_CollectDataGetModel get(Map<String, String> header, Integer pressureMonitor1Id, Map<String, Object> query) throws ServiceException {
        /* 查询压力检测仪：查询加装在用户管辖范围内的调压箱柜上的压力检测仪，如果不存在则抛出异常“压力监测仪不存在” */
        IotDeviceCustomQueryModel iotDeviceCustomQueryModel = new IotDeviceCustomQueryModel();
        iotDeviceCustomQueryModel.setEqual_categoryCode("5");//压力监测仪
        String userCode = getUserCode(header);
        iotDeviceCustomQueryModel.setLike_collectDeviceExt_resourceCode(userCode + "%");
        iotDeviceCustomQueryModel.setEqual_id(pressureMonitor1Id);
        int count = iotDeviceDao.selectCount(iotDeviceCustomQueryModel);
        if (count == 0) {
            throw new ServiceException(Device_ErrorsEnum.PRESSUREMONITOR1_COLLECTDATASERVICE_GET_201);
        }

        /* 查询压力检测仪所采集的数据(根据采集时间降序排列) */
        RegulatorCollectDataCustomQueryModel queryModel = regulatorCollectDataCustomConvertor.pressureMonitor1_CollectDataService_query2queryModel(query);
        queryModel.setEqual_isValid("1");
        queryModel.setEqual_iotDeviceId(pressureMonitor1Id);
        queryModel.setOrderBy_collectTime("desc");

        String show = HzhrTypeUtil.object2string(query.get("_show"));
        List<RegulatorCollectDataEntityExt> extList = null;
        if (!StringUtils.isEmpty(show)) {
            extList = regulatorCollectDataDao.selectExtList(queryModel);
        }

        Boolean showTotal = HzhrTypeUtil.object2boolean(query.get("_show_total"));
        Integer total = null;
        if (showTotal != null && showTotal) {
            total = regulatorCollectDataDao.selectCount(queryModel);
        }

        /* 返回信息 */
        PressureMonitor1_CollectDataGetModel result = regulatorCollectDataCustomConvertor.extList2pressureMonitor1_CollectDataGetModel(extList, total, null, query);
        return result;
    }
}

